package com.project.core.util;

import cn.hutool.core.util.StrUtil;
import com.project.cache.helper.CacheProxy;
import com.project.core.auth.LoginRes;
import com.project.core.constant.ScoreConstant;
import com.project.core.entity.User;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author mengxj
 */
public class ScoreContext {

    private static  CacheProxy cache = new CacheProxy();

    public static User currentUser(){
        try {
            HttpServletRequest request = CommonUtil.getRequest();
            String token = request.getHeader(ScoreConstant.ACCESS_TOKEN);
            if(StrUtil.isEmpty(token)){
                return new User();
            }
            User user = cache.get(token, LoginRes.class).getUser();
            if(Objects.isNull(user)){
                return new User();
            }
            return user;
        }catch (Exception e){
            return new User();
        }
    }
}
